<?php
/**
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     @license http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

$currentDir = dirname(__FILE__);
require_once($currentDir . "/sensapilityservice.php");
require_once($currentDir . "/utilities.php");

/**
 * @author api.jmatt@gmail.com
 * @version 0.1
 */

class SensapilityAfc extends SensapilityService{
  
  /**
   * @param soapclientnusoap $soapClient the NuSOAP object that communicates 
   * with the service
   * @param array $namespaces associative array that matches namespace labels
   * to namespace URIs
   * @param array $defaultNamespaceLabel the namespace to be used with most
   * tags
   * @return none
   */
  function SensapilityAFC($soapClient, $namespaces, $defaultNamespaceLabel) {
    parent::SensapilityService($soapClient, 
                               $namespaces,
                               $defaultNamespaceLabel);
  }


  /**
   * Returns the ad code JavaScript snippet to generate ads with the requested
   * parameters.
   * @param String $serviceId the id of the publisher for the AdSenseForContent
   * service
   * @param array $adStyle associative array of values representing the 
   * ad style object defined 
   * {@link http://code.google.com/apis/adsense/developer/AdStyle.html here}
   * @param string $adUnitType the type of ad unit, eg. TextOnly, ImageOnly,
   * etc
   * @param string $layout the dimensions of the ad unit
   * @param string $alternate if this ad code would display a PSA the 
   * alternate parameter allows you to specify that a block of a given color
   * should be display, or an arbitrary image. To specify a color, pass in 
   * a hexadecimal string beginning with #. To specify an arbitrary image, 
   * pass in a URL
   * @param boolean $isFramedPage whether the ad code runs in the context of
   * a frame
   * @param string $channelName the name of the custom channel this ad belongs
   * to, if there is no channel, this should be null
   * @return string the JavaScript ad code
   *  
   */
  function generateAdCode($serviceId, 
                          $adStyle, 
                          $adUnitType, 
                          $layout, 
                          $alternate, 
                          $isFramedPage, 
                          $channelName) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;

    if ( $alternate ) {
      $isFramedPage = 'true';
    } else {
      $isFramedPage = 'false';
    }

    $styleArg = array();
    $styleArg['value'] = $adStyle;
    $callParameters = array($ns . ':synServiceId' => $serviceId,
                            $ns . ':adStyle' => $styleArg,
                            $ns . ':adUnitType' => $adUnitType,
                            $ns . ':layout' => $layout,
                            $ns . ':alternate' => $alternate,
                            $ns . ':isFramedPage' => $isFramedPage,
                            $ns . ':channelName' => $channelName);

    return parent::doSoapCall('generateAdCode',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              0,
                              false);
  }

  /**
   *
   * Get channels for a given client
   * @param string $serviceId the id of the publisher for the AdSenseForContent
   * service
   * @param string $type whether to retreive URL or Custom channels
   * @return Channel_Data[] 
   * {@link http://code.google.com/apis/adsense/developer/ChannelService_Data.html ChannelService_Data[]} 
   * array of ChannelService_Data objects that are all channels for this client
   */
  function getChannels($serviceId, $type) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = array($ns . ':synServiceId' => $serviceId,
                            $ns . ':type' => $type);

    return parent::doSoapCall('getChannels',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              2,
                              true);
  }

  /*
   * Save an ad style
   * @param string $serviceId the id of the publisher for the AdSenseForContent
   * service
   * @param string $backgroundColor hexadecimal string starting with '#' that
   * represents the background color of the ad block
   * @param string $borderColor hexadecimal string starting with '#' that
   * represents the border color of the ad block
   * @param string $name the name to save the style as
   * @param string $textColor hexadecimal string starting with '#' that
   * represents the text color of the ad block
   * @param string $titleColor hexadecimal string starting with '#' that
   * represents the title color of the ad block
   * @param string $urlColor hexadecimal string starting with '#' that
   * represents the color of any URLs in the ad
   * @return none
   */
  function saveAdStyle($serviceId, 
                       $backgroundColor, 
                       $borderColor, 
                       $name, 
                       $textColor, 
                       $titleColor, 
                       $urlColor) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = array($ns . ':synServiceId' => $serviceId);
    $styleParams = array("value" => array($ns . ':backgroundColor' =>
                                              $backgroundColor,
                                          $ns . ':borderColor' => $borderColor,
                                          $ns . ':name' => $name,
                                          $ns . ':textColor' => $textColor,
                                          $ns . ':titleColor' => $titleColor,
                                          $ns . ':urlColor' => $urlColor)
                         );
    $callParameters[$ns . ':adStyle'] = $styleParams;

    return parent::doSoapCall('saveAdStyle',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              0,
                              false,
                              true);
  }

  /**
   * Save multiple ad styles in one call
   * @param string $serviceId the id of the publisher for the AdSenseForContent
   * service
   * @param array $adStyle array of 
   * {@link http://code.google.com/apis/adsense/developer/AdStyle.html AdStyle}
   * objects (or associative arrays)
   * @return none
   *  
   */
  function saveAdStyles($serviceId, $adStyle) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = ArrayToXML(array($ns . ':synServiceId' => $serviceId));
    $keys = array_keys($adStyle);

    foreach ( $keys as $key ) {
      $thisStyle = $adStyle[$key];
      $styleParams = array("value" => array($ns . ':backgroundColor' =>
                                                $thisStyle['backgroundColor'],
                                            $ns . ':borderColor' => 
                                                $thisStyle['borderColor'],
                                            $ns . ':name' => $thisStyle['name'],
                                            $ns . ':textColor' => 
                                                $thisStyle['textColor'],
                                            $ns . ':titleColor' => 
                                                $thisStyle['titleColor'],
                                            $ns . ':urlColor' => 
                                                $thisStyle['urlColor'])
                           );
      $styleParams = array($ns . ':adStyles' => $styleParams);
      $callParameters .= arrayToXML($styleParams);
    }

    return parent::doSoapCall('saveAdStyles',
                              $callParameters,
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }

  /**
   * Get a named ad style (custom or built-in)
   * @param string $serviceId the id of the publisher for the AdSenseForContent
   * service
   * @param string $styleName the name of the style to retrieve
   * @return AdStyle the 
   * {@link http://code.google.com/apis/adsense/developer/AdStyle.html AdStyle object} for the named style
   *
   */
  function getAdStyle($serviceId, $styleName) {
    $allStyles = $this->getAdStyles($serviceId);
    if ( is_null($allStyles) ) {
      return null;
    }
    $keys = array_keys($allStyles);
    $counter = -1;
    $found = false;
    foreach ($keys as $key) {
      if ( !$found ) {
        if ( $styleName == $allStyles[$key]['name'] ) {
          $found = true;
        }
        $counter += 1;
      }
    }
    if ( $found ) {
      return $allStyles[$keys[$counter]];
    } else {
      return null;
    }
  }

  /**
   * Get multiple named (custom or built-in) ad styles with one request
   * @param string $serviceId the id of the publisher for the AdSenseForContent
   * service
   * @return AdStyle[] the 
   * {@link http://code.google.com/apis/adsense/developer/AdStyle.html AdStyle objects} for the client
   */
  function getAdStyles($serviceId) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = array($ns . ':synServiceId' => $serviceId);

    return parent::doSoapCall('getAdStyles',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              2,
                              true);
  }

  /**
   * Delete a named ad style
   * @param string $serviceId the id of the publisher for the AdSenseForContent
   * service
   * @param string $name the name of the style to delete
   * @return none
   * 
   */
  function deleteAdStyle($serviceId, $name) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = array($ns . ':synServiceId' => $serviceId,
                            $ns . ':name' => $name);

    return parent::doSoapCall('deleteAdStyle',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }

  /**
   * Delete a group of named ad styles
   * @param string $serviceId the id of the publisher for the AdSenseForContent
   * service
   * @param string[] $names the names of the styles to delete
   * @return none
   * 
   */
  function deleteAdStyles($serviceId, $names) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = array($ns . ':synServiceId' => $serviceId);
    $callParameters = ArrayToXML($callParameters);

    $nameKeys = array_keys($names);
    foreach ( $nameKeys as $thisKey ) {
      $tmpArray = array($ns . ':names' => $names[$thisKey]);
      $callParameters .= ArrayToXML($tmpArray);
    }

    return parent::doSoapCall('deleteAdStyles',
                              $callParameters,
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }

  /**
   * Get all the add layouts
   */
  function getAllAdLayouts() {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);

    return parent::doSoapCall('getAllAdLayouts',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              1,
                              true);
  }

  /**
   * Get ad layouts for a given type of ad unit
   * @param string $unitType the type of ad unit (eg. TextOnly)
   * @return string[] the layouts compatible with this ad unit type
   */
  function getAdLayouts($unitType) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = array($ns . ':adUnitType' => $unitType);

    return parent::doSoapCall('getAdLayouts',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              1,
                              true);
  }

  /**
   * Get all ad unit types
   * @return string[] all available ad unit types
   */
  function getAllAdUnitTypes() {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);

    return parent::doSoapCall('getAllAdUnitTypes',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              1,
                              true);
  }

  /**
   * Get ad unit types for a given layout
   * @param string $layout the layout to get the unit types for
   * @return string[] the ad unit types usable with this layout
   */
  function getAdUnitTypes($layout) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = array($ns . ':adLayout' => $layout);

    return parent::doSoapCall('getAdUnitTypes',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              1,
                              true);
  }

  /**
   * Get all the built-in ad styles
   * @return AdStyle[] 
   * {@link http://code.google.com/apis/adsense/developer/AdStyle.html AdStyle[]}
   * the built-in ad style
   */
  function getBuiltinAdStyles() {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);

    return parent::doSoapCall('getBuiltinAdStyles',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              2,
                              true);
  }


  /**
   * @param string $serviceId the id of the publisher for the AdSenseForContent
   * service
   * @param string $type whether to get Url or Custom channels
   * @return ChannelService_Data[] 
   * {@link http://code.google.com/apis/adsense/developer/ChannelService_Data.html ChannelService_Data[]} 
   * The array of active channels
   */
  function getActiveChannels($serviceId, $type) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = array($ns . ':synServiceId' => $serviceId,
                            $ns . ':type' => $type);

    return parent::doSoapCall('getActiveChannels',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              2,
                              true);
  }

  /**
   * @param string $serviceId the id of the publisher for the AdSenseForContent
   * service
   * @param string whether to get Url or Custom channels
   * @return ChannelService_Data[] 
   * {@link http://code.google.com/apis/adsense/developer/ChannelService_Data.html ChannelService_Data[]} 
   * The array of inactive channels
   */
  function getInactiveChannels($serviceId, $type) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = array($ns . ':synServiceId' => $serviceId,
                            $ns . ':type' => $type);

    return parent::doSoapCall('getInactiveChannels',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              2,
                              true);
  }
}
?>
